package com.bz.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.dao.BannerDao;
import com.bz.dao.CustomDao;
import com.bz.dao.ServiceDao;
import com.bz.entity.BannerEntity;
import com.bz.entity.CustomEntity;
import com.bz.entity.ServiceEntity;
import com.bz.model.CustomDto;
import com.bz.model.HomeDto;
import com.bz.model.ServiceDetailDto;
import com.bz.model.ServiceDto;
import com.bz.service.IHomeService;

@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private BannerDao  bannerDao;

    @Autowired
    private CustomDao  customDao;

    @Autowired
    private ServiceDao serviceDao;

    @Override
    public HomeDto info() {
        HomeDto home = new HomeDto();
        home.setBanner(getbanner());
        home.setService(getService());
        home.setCustom(getcustom());
        return home;
    }

    private String getbanner() {
        LambdaQueryWrapper<BannerEntity> query = new LambdaQueryWrapper<>();
        query.eq(BannerEntity::getType, "1"); // 1:home
        query.orderByDesc(BannerEntity::getUpdateDate).last("limit 0,1");
        BannerEntity db = bannerDao.selectOne(query);
        if (null == db) {
            return null;
        }
        return db.getImg();
    }

    private List<ServiceDto> getService() {
        // 服务分类 1:项目材料采购及检查 n2:QA/QC工厂检查
        List<ServiceDto> list = new ArrayList<>();
        ServiceDto service1 = new ServiceDto();
        service1.setName("项目材料采购及检查 ");
        ServiceDto service2 = new ServiceDto();
        service2.setName("A/QC工厂检查");
        list.add(service1);
        list.add(service2);
        LambdaQueryWrapper<ServiceEntity> query = new LambdaQueryWrapper<>();
        query.orderByDesc(ServiceEntity::getSort).orderByDesc(ServiceEntity::getUpdateDate);
        List<ServiceEntity> datas = serviceDao.selectList(query);
        if (CollectionUtils.isEmpty(datas)) {
            return list;
        }
        service1.setDatas(datas.stream().filter(it -> {
            return StringUtils.equals("1", it.getType());
        }).map(it -> {
            ServiceDetailDto d = new ServiceDetailDto();
            d.setImg(it.getImg());
            d.setTitle(it.getTitle());
            return d;
        }).collect(Collectors.toList()));
        service2.setDatas(datas.stream().filter(it -> {
            return StringUtils.equals("2", it.getType());
        }).map(it -> {
            ServiceDetailDto d = new ServiceDetailDto();
            d.setImg(it.getImg());
            d.setTitle(it.getTitle());
            return d;
        }).collect(Collectors.toList()));

        return list;
    }

    private List<CustomDto> getcustom() {
        LambdaQueryWrapper<CustomEntity> query = new LambdaQueryWrapper<>();
        query.orderByDesc(CustomEntity::getSort).orderByDesc(CustomEntity::getUpdateDate);
        List<CustomEntity> datas = customDao.selectList(query);
        if (CollectionUtils.isEmpty(datas)) {
            return null;
        }
        return datas.stream().map(it -> {
            CustomDto c = new CustomDto();
            c.setName(it.getName());
            c.setImg(it.getImg());
            return c;
        }).collect(Collectors.toList());
    }
}
